package com.quanquan.user.message;

import com.quanquan.user.request.IntegralDetailsRequestBody;
import com.quanquan.user.service.IntegralTradingDetailsService;
import com.quanquan.user.service.TaskRecordService;
import com.rabbitmq.client.Channel;
import commond.IntegralOutPut;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;

/**
 * 功能描述:接收其他模块传来的消费积分的数据
 *
 * @ClassName: MqImageReceiver
 * @Author: xiaoqianh丶
 * @Date: 2019-03-13 14:07
 * @Version: V1.0
 */
@Slf4j
@Component
public class MqTaskReceiver {

    private org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

private final TaskRecordService taskRecordService;

    public MqTaskReceiver(TaskRecordService taskRecordService) {
        this.taskRecordService = taskRecordService;
    }




    @RabbitListener(bindings = {@QueueBinding(
            value = @Queue(value = "task",
                    durable = "true"),
            exchange = @Exchange(value = "task.exchange",
                    type = "topic"),
            ignoreDeclarationExceptions = "true",
            key = "task.#")})
    @RabbitHandler
    public void completionIntegralBalance(Message message, Channel channel) throws IOException {
        String userId = Objects.requireNonNull(message.getHeaders().get("userId")).toString();
        String taskType = Objects.requireNonNull(message.getHeaders().get("taskType")).toString();

        //手工ack
        long deliveryTag= Long.parseLong(Objects.requireNonNull(message.getHeaders().get(AmqpHeaders.DELIVERY_TAG)).toString());
        channel.basicAck(deliveryTag,false);

        taskRecordService.addTaskRecord(userId,taskType);


    }


}
